function out = estimate_fun(data1,conv_ma,I,ng,param_ini)

% modified for RAND round 1 revision on June 25, 2022

objfun0 = @(x) -ll_fun5([x(1); x(2); x(3); x(4); conv_ma*x(5:ng+4)';...
    conv_ma*x(5+ng:2*ng+4)';...
    x(2*ng+5:2*ng+6)'; x(2*ng+7)],data1,I);

%% do the search 4 times.. 
% first trail using user specified initial guess
options = optimset('MaxFunEvals',600,'MaxIter',600,...
    'TolFun',.00001,'TolX',.00001);
x1 = fminsearch(objfun0,param_ini,options);  % vector of estimated parameters
% second use the estimates from first trial as starting value for the next
options = optimset('MaxFunEvals',600,'MaxIter',600,...
    'TolFun',.00001,'TolX',.00001);
x2 = fminsearch(objfun0,x1,options);
% third  use the estimates from second as starting guess
options = optimset('MaxFunEvals',1500,'MaxIter',1500,...
    'TolFun',.00001,'TolX',.00001);
x3 = fminsearch(objfun0,x2,options);
% fourth and final 
options = optimset('MaxFunEvals',1500,'MaxIter',1500,...
    'TolFun',.00001,'TolX',.00001);
x = fminsearch(objfun0,x3,options);
%%
[objvalue, lowuest] = ll_fun5([x(1); x(2); x(3); x(4); conv_ma*x(5:ng+4)'; conv_ma*x(5+ng:2*ng+4)';...
    x(2*ng+5:2*ng+6)'; x(2*ng+7)],data1,I);

out = [x lowuest objvalue];

end

